﻿#include "SceneContentTest1.h"
#include "3dMath/ss3dMathFunction.h"
//#include "element/camera/ssThirdPersonCamera.h"
#include "element/render/ssMeshRender.h"
#include "element/material/ssMaterial.h"
#include "element/material/ssTextureMaterial.h"
#include "element/render/ssAxesRender.h"
//#include "element/render/ssColorRender.h"
#include "shader/ssShaderManager.h"
#include "console/ssConsole.h"
#include "node/ssNodeFactory.h"
#include "resmgr/ssResManager.h"


SceneContentTest1::SceneContentTest1(ssSceneManger& scene)
	:BasicSceneContent(scene)
{
	m_moveNode = 0;
}

SceneContentTest1::~SceneContentTest1(void)
{
}

void SceneContentTest1::InitNode()
{
	StarSeeker::ssNode* pAxesRE = SS_NODE_FACTORY->CreateAxesNode();
	m_scene.RootNode().AddChild(pAxesRE);

	StarSeeker::ssNode* pTextureBdNode = SS_NODE_FACTORY->CreateBoardNode("WM.tga");
	m_scene.RootNode().AddChild(pTextureBdNode);
	m_moveNode = pTextureBdNode;
	StarSeeker::ssNode* pTextureBdNode2 = SS_NODE_FACTORY->CreateBoardNode("WM.tga", 1.0f, 1.0f);
	pTextureBdNode2->SetName("board2");
	pTextureBdNode->AddChild(pTextureBdNode2);
	StarSeeker::ssNode* pTextureBdNode3 = SS_NODE_FACTORY->CreateBoardNode("WM.tga", 1.0f, 1.0f);
	pTextureBdNode2->SetName("board3");
	pTextureBdNode2->AddChild(pTextureBdNode3);

	StarSeeker::ssNode* pBillboardNode = SS_NODE_FACTORY->CreateBillboardNode("UINV.tga");
	m_scene.RootNode().AddChild(pBillboardNode);

	StarSeeker::ssNode* pTextureMdNode = SS_NODE_FACTORY->CreateTextureModelNode("yunshi_01 DM.tga", "yunshi001.OBJEX");
	m_scene.RootNode().AddChild(pTextureMdNode);

	StarSeeker::ssNode* pTeapotNode = SS_NODE_FACTORY->CreateModelNode("teapot.obj");
	pTextureMdNode->AddChild(pTeapotNode);

	StarSeeker::ssNode* pTerrainNode = SS_NODE_FACTORY->CreateTerrain10Node("hight.tga", "clay.tga", "meadow.tga");
	m_scene.RootNode().AddChild(pTerrainNode);

	SS_RES_MANAGR->CreateTexture2d("R2T", 800, 600);
	StarSeeker::ssTexture2D* pTexture = new StarSeeker::ssTexture2D;
	pTexture->Load("R2T");
	StarSeeker::ssNode* pRTNode = SS_NODE_FACTORY->CreateBoardNode(pTexture);
	pRTNode->SetName("渲染到纹理");
	m_scene.RootNode().AddChild(pRTNode);
	m_camera->Render2Texture(GL_COLOR_ATTACHMENT0, pTexture->GetHandle(0));

	ssQuaternion qt;

	pTextureBdNode->Transform().SetPosition(100.0f, 0.0f, 0.0f);
	pTextureBdNode2->Transform().SetPosition(0, 50, 50);
	pTextureBdNode3->Transform().SetPosition(0, 50, 50);
	qt.SetAxisAngle(ssVector3(0, 0, 1), Math3D::gc_HALF_PI);
	pTextureBdNode->Transform().SetRotation(qt);

	//qt.SetEuler(0, 0, Math3D::gc_QUARTER_PI);
	//pAxesRE->Transform().SetRotation(qt);
	//pAxesRE->Transform().SetScaling(0.5f, 0.5f, 0.5f);

	pBillboardNode->Transform().SetPosition(-120.0f, 0.0f, 0.0f);

	pTextureMdNode->Transform().SetPosition(0.0f, 100.0f, 0.0f);
	pTeapotNode->Transform().SetPosition(0.0f, 0.0f, -100.0f);
	pRTNode->Transform().SetPosition(0.0f, 0.0f, -100.0f);

	//pTextureBdNode->Transform().ComputeViewMatrix();
	//ssFLOAT3X up1 = pTextureBdNode->Transform().Up();
	//SS_CONSOLE->Printf("up1: %f,%f,%f\n", up1.x, up1.y, up1.z);
	//ssFLOAT3X left1 = pTextureBdNode->Transform().Left();
	//SS_CONSOLE->Printf("left1: %f,%f,%f\n", left1.x, left1.y, left1.z);
	//ssFLOAT3X forward1 = pTextureBdNode->Transform().Forward();
	//SS_CONSOLE->Printf("forward1: %f,%f,%f\n", forward1.x, forward1.y, forward1.z);
}

void SceneContentTest1::OnLeftUp(i32 x, i32 y)
{
	y = SS_OPENGL->m_nHeight - y;
	SS_CONSOLE->Printf("left up %d,%d\n", x, y);
	ssVector3 p1(x, y, -1.0f);
	ssVector3 p2(x, y, 1.0f);
	ssViewPort port;
	port.x = port.y = 0; port.width = SS_OPENGL->m_nWidth; port.height = SS_OPENGL->m_nHeight;
	ssVector3 pnear;
	Math3D::ScreenToWorld(&p1, &m_scene.MainCamera()->GetViewMatrix(), &m_scene.MainCamera()->GetProjMatrix(), &port, &pnear);
	ssVector3 pfar;
	Math3D::ScreenToWorld(&p2, &m_scene.MainCamera()->GetViewMatrix(), &m_scene.MainCamera()->GetProjMatrix(), &port, &pfar);

	StarSeeker::ssNode* pLineNode = SS_NODE_FACTORY->CreateLineNode(pnear, pfar, ssColor4(1.0f, 0.0f, 0.0f, 1.0f));
	m_scene.RootNode().AddChild(pLineNode);
}

void SceneContentTest1::OnKeyDown(i32 k)
{
	BasicSceneContent::OnKeyDown(k);
	ssFLOAT3X pos = m_moveNode->Transform().GetPosition();
	switch (k)
	{
	case 74://j
		pos.x -= 10;
		m_moveNode->Transform().SetPosition(pos.x, pos.y, pos.z);
		break;
	case 76://l
		pos.x += 10;
		m_moveNode->Transform().SetPosition(pos.x, pos.y, pos.z);
		break;
	}
}